{% extends "master.html" %}
<meta charset="UTF-8">

{% block master %}
{% csrf_token %}
<div class="x-nav">
	<span class="layui-breadcrumb">
        <a href="">用户注册管理</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
      </a>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="id_email" name="email" placeholder="邮箱地址" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select name="contrller" id ='id_status'>
                                <option value="">审批状态</option>
                                <option value="0">待审批</option>
                                <option value="1">审批通过</option>
                                <option value="2">审批拒绝</option>
                            </select>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select class="container" id="id_is_use">
                                <option value="">使用状态</option>
                                <option value="True">已使用</option>
                                <option value="False">待使用</option>
                            </select>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select class="container" id="id_is_check">
                                <option value="">审批结果</option>
                                <option value="True">已审批</option>
                                <option value="False">待审批</option>
                            </select>
                        </div>
                    </form>
                    <div class="layui-input-inline layui-show-xs-block UserTable">
                        <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="sreach">
                            <i class="layui-icon">&#xe615;</i></button>
                    </div>
                </div>
                <xblock>
                    <!--
                    <div class="layui-card-header UserTable">
                        <button class="layui-btn " data-type="getCheckData">
                            <i class="layui-icon layui-icon-close-fill"></i>禁用注册
                        </button>
                    </div>
                    -->
                </xblock>
                <!-- user table-->
                <div class="layui-card-body">
                    <table class="layui-table layui-form" id="user_table" lay-filter="user"></table>
                </div>
                <!--end user table-->
            </div>
        </div>
    </div>
</div>

<script>
var csrf = $('input[name="csrfmiddlewaretoken"]').val();
layui.use('table', function(){
  var table = layui.table;
	
  //方法级渲染
  table.render({
    elem: '#user_table'
    ,url: "{% url 'userregistlist' %}"
    ,method: 'post'
    ,where: {'csrfmiddlewaretoken':csrf}
    ,cols: [[
      {checkbox: true, fixed: true}
      ,{field:'email', title: '申请邮箱', sort: true, fixed: true}
      ,{field:'is_check', title: '是否审批'}
      ,{field:'is_use', title: '是否使用', sort: true}
      ,{field:'request_type', title: '申请类型'}
      ,{field:'starttime', title: '申请时间'}
      ,{field:'action_user', title: '审批人'}
      ,{field:'updatetime', title: '审批时间'}
      ,{ title: '审批操作',fixed: 'right', align:'center', toolbar: '#barDemo'}
    ]]
    ,id: 'testReload'
    ,page: true
    ,text:{
    	none:'暂无相关数据'
    }
  });
  // 操作栏功能
  table.on('tool(user)', function(obj){
    var data = obj.data;
    if(obj.event === 'access'){
    	layer.confirm('该操作为敏感操作<br>点击确认，同意用户'+data.email+'注册为 '+data.request_type,function(index){
    		$.post("{% url 'userregistaction' %}",{'request_id':data.request_id,'action':'access','csrfmiddlewaretoken': csrf},function(res){
        		layer.msg(res.error);
        		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
        	});
    	})
    } else if(obj.event === 'deny'){
    	layer.confirm('该操作为敏感操作<br>点击确认，拒绝用户'+data.email+'注册为申请',function(index){
    		$.post("{% url 'userregistaction' %}",{'request_id':data.request_id,'action':'deny','csrfmiddlewaretoken': csrf},function(res){
        		layer.msg(res.error);
        		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
        	});
    	})
    }
  });
  // 注册页功能
  var $ = layui.$, active = {
    getCheckData: function(){ //获取选中数据
	    var checkStatus = table.checkStatus('testReload')
	    ,data = checkStatus.data
	    ,list= new Array();
    	for(var i=0;i<data.length;i++){
    		list[i] = data[i].request_id
    	}
    	layer.confirm('该操作为敏感操作<br>点击确认，禁用所选用户的注册权限',function(index){
		    $.post("{% url 'userregiststop' %}",{'regist_id_list':JSON.stringify(list),'action':'stop','csrfmiddlewaretoken': csrf},function(res){
	    		layer.msg(res.error);
	    		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
	    	});
    	})
    }
    ,reload: function() {
        //执行重载
        //console.log($("#id_is_check option:selected").val());
        //console.log($("#id_is_use option:selected").val());
        //console.log($("#id_status option:selected").val());
        table.reload('testReload', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                email: $('#id_email').val()
                , is_check: $("#id_is_check option:selected").val()
                , is_use: $("#id_is_use option:selected").val()
                , status: $("#id_status option:selected").val()
            }
        });
        }
  };
  $('.UserTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>

<script type="text/html" id="barDemo">
	{% verbatim %}
	{{# if(d.status=='待审批'){ }}
		<a class="layui-btn layui-btn-xs" lay-event="access">同意</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="deny">拒绝</a>
	{{# } else { }}
		{{ d.status }}
	{{# } }}
	{% endverbatim %}
</script>
{% endblock %}